package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Record;
import org.one.afternoon.provider.xunfei.T2AListener;
import org.one.afternoon.service.RecordService;
import org.one.afternoon.provider.baidu.MyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.MediaType;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/text2audio")
@Slf4j
public class Text2AudioController {

    @Autowired
    private RecordService recordService;

    @Resource
    MyClient myClient;

    public static final String tok = "tok=25.8115243c95c51b21b338b7979200a758.315360000.2012692531.282335 - 41099492";
    public static final String ctp = "1";
    public static final String lan = "zh";


    @CrossOrigin
    @PostMapping("/convert/baidu")
    @ResponseBody
    public Result<Byte[]> getAudioByBaidu(@RequestParam String text) {
        Map<String, Object> map = new HashMap<>();
        map.put("tok", tok);
        map.put("ctp", ctp);
        map.put("lan", lan);
        map.put("tex", text);
        map.put("spd", 4);
        map.put("pit", 5);
        map.put("vol", 5);
        map.put("per", 5118);
        map.put("aue", 3);
        map.put("cuid", "MNOSvF72O7JZ2KrqZAnIEbY4KBn3repX");

        Byte[] bytes = myClient.t2v(map);
        return Result.success(bytes);
    }

    @CrossOrigin
    @PostMapping(value = "/convert/kdxf")
    public ResponseEntity<byte[]> getAudioByKdxf(@RequestParam String tex) {

        LambdaQueryWrapper<Record> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(tex), Record::getAnswer, tex);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("audio/mp3"));

        log.info("①:音频处理阶段缓存机制开启...");
        if (recordService.list(lambdaQueryWrapper) == null || recordService.list(lambdaQueryWrapper).isEmpty()) {
            log.info("②:并未在数据库中查找到相似数据...");
            try {
                T2AListener.connect(tex);
                log.info("③:已与服务器建立连接...");
            } catch (Exception e) {
                log.error(e.getMessage());
            }
            while (true) {
                if (T2AListener.end) {
                    T2AListener.end = false;
                    log.info("成功获得音频流数据,并开始保存...");
                    LambdaUpdateWrapper<Record> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                    lambdaUpdateWrapper.set(T2AListener.result != null, Record::getAudio, T2AListener.result).eq(StringUtils.isNotBlank(tex), Record::getAnswer, tex);
                    recordService.update(lambdaUpdateWrapper);
                    log.info("②:音频流数据保存成功...");
                    return new ResponseEntity<>(T2AListener.result, headers, HttpStatus.OK);
                }
            }
        }

        log.info("已从数据库拿到相似答案的音频数据...");
//        return Result.success(recordService.list(lambdaQueryWrapper).get(0).getAudio());
        return new ResponseEntity<>(recordService.list(lambdaQueryWrapper).get(0).getAudio(), headers, HttpStatus.OK);
    }
}
